﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace VASMide
{
    public partial class filebrowser : Form
    {
        public filebrowser()
        {
            InitializeComponent();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
           // listBox1.Items.Clear();
            foreach (string s in Project.SourceFiles)
            {
               // listBox1.Items.Add(Path.GetFileName(s));
            }
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
            
        }
        public void refreshItems()
        {
            ImageList il = new ImageList();
            il.Images.Add(Properties.Resources.script);
            il.Images.Add(Properties.Resources.dll);
            il.Images.Add(Properties.Resources.vicon);
            treeView1.Nodes[1].Nodes.Clear();
            treeView1.Nodes[0].Nodes.Clear();

            treeView1.ImageList = il;
            foreach (string s in Project.SourceFiles)
            {
                TreeNode tn = new TreeNode();
                tn.Text = Path.GetFileName(s);
                tn.ImageIndex = 2;
                tn.SelectedImageIndex = 2;
                treeView1.Nodes[1].Nodes.Add(tn);
            }
            foreach (string s in Project.References)
            {
                TreeNode tn = new TreeNode();
                tn.Text = Path.GetFileName(s);
                tn.ImageIndex = 1;
                tn.SelectedImageIndex = 1;
                treeView1.Nodes[0].Nodes.Add(tn);
            }
        }
        private void filebrowser_Load(object sender, EventArgs e)
        {
            timer1.Start();
            ImageList il = new ImageList();
            il.Images.Add(Properties.Resources.script);
            il.Images.Add(Properties.Resources.dll);
            il.Images.Add(Properties.Resources.vicon);
            il.Images.Add(Properties.Resources.vcl);
            treeView1.ImageList = il;
            foreach (string s in Project.SourceFiles)
            {
                TreeNode tn = new TreeNode();
                tn.Text = Path.GetFileName(s);
                tn.ImageIndex = 2;
                tn.SelectedImageIndex = 2;
                treeView1.Nodes[1].Nodes.Add(tn);
            }
            foreach (string s in Project.References)
            {
                TreeNode tn = new TreeNode();
                tn.Text = Path.GetFileName(s);
                tn.ImageIndex = 3;
                tn.SelectedImageIndex = 3;
                treeView1.Nodes[0].Nodes.Add(tn);
            }
        }
        string old_p = "";
        public string SelectedFile = "";
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                if (e.Node.Parent.Text == "Code")
                {
                    foreach (string s in Project.SourceFiles)
                    {
                        if (s.EndsWith(@"\" + e.Node.Text))
                        {
                            if (old_p != "")
                            {
                                StreamWriter sw = new StreamWriter(old_p);
                                sw.Write(Form1.c.fastColoredTextBox1.Text);
                                sw.Close();
                            }
                            old_p = s;
                            Form1.c.fastColoredTextBox1.Text = File.ReadAllText(s);
                            SelectedFile = s;
                        }
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            treeView1.Height = this.Height - toolStrip1.Height;
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            this.refreshItems();
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {

        }
    }
}
